package com.yyjj.yyjj.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.javafx.tk.PermissionHelper;
import com.yyjj.yyjj.acl.mapper.PermissionMapper;
import com.yyjj.yyjj.acl.service.PermissionService;
import com.yyjj.yyjj.acl.service.RolePermissionService;
import com.yyjj.yyjj.acl.service.RoleService;
import com.yyjj.yyjj.acl.utils.PermissionHelper2;
import com.yyjj.yyjj.common.result.Result;
import com.yyjj.yyjj.model.acl.Permission;
import com.yyjj.yyjj.model.acl.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService  {


    @Autowired
    private RolePermissionService permissionService;


    @Override
    public List<Permission> queryAllPermission() {
        List<Permission> permissionList = baseMapper.selectList(null);
        List<Permission> result= PermissionHelper2.build(permissionList);
        return result;


    }

    @Override
    public void removeChildById(Long id) {
        List<Long> idList=new ArrayList<>();
        this.getAllPermissionId(id,idList);
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
    }

    private void getAllPermissionId(Long id, List<Long> idList) {
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid,id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        childList.stream().forEach(item->{
            idList.add(item.getId());
            this.getAllPermissionId(item.getId(),idList);
        });
    }


    /**
     * 分配菜单
     * @param roleId
     * @param permissionIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds){
        //删除用户分配的角色数据
        permissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));

        //分配新的角色
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for(Long permissionId : permissionIds) {
            if(StringUtils.isEmpty(roleId)) continue;
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        permissionService.saveBatch(rolePermissionList);
    }

    /**
     * 根据用户获取菜单数据
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findPermissionByRoleId(Long roleId) {
        //查询所有的角色
        List<Permission> allPermissionList =queryAllPermission();
        List<Permission> assignPermission1 =baseMapper.selectList(null);

        //拥有的角色id
        List<RolePermission> existRolePermissionList = permissionService.list(new QueryWrapper<RolePermission>().eq("role_id", roleId).select("permission_id"));
        List<Long> existPermissionList = existRolePermissionList.stream().map(c->c.getPermissionId()).collect(Collectors.toList());

        //对角色进行分类
        List<Permission> assignPermission2 = new ArrayList<Permission>();
        for (Permission permission : assignPermission1) {
            //已分配
            if(existPermissionList.contains(permission.getId())) {
                assignPermission2.add(permission);
            }
        }
        List<Long> assignPermission = assignPermission2.stream().map(c->c.getId()).collect(Collectors.toList());

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignPermission", assignPermission);
        roleMap.put("allPermissionList", allPermissionList);
        return roleMap;
    }




}
